package com.magicborrow.utils;

import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.magicborrow.MagicBorrowApplication;


import org.json.JSONObject;

import java.text.DecimalFormat;
import java.util.HashMap;


public class StringUtil {
    public static String double2String(double d) {
        DecimalFormat format = new DecimalFormat("#####0.0");
        return format.format(d);

    }

    public static String double2String2(double d) {
        DecimalFormat format = new DecimalFormat("#####0.00");
        return format.format(d);

    }

    public static String float2String(float d) {
        DecimalFormat format = new DecimalFormat("#####0.00");
        return format.format(d);

    }

    public static JSONObject mapToJson(HashMap<String, Object> map) {
        if (map == null || map.isEmpty()) {
            return null;
        }

        Gson gson = new Gson();
        String s = gson.toJson(map);
        JSONObject js = null;
        try {
            js = new JSONObject(s);

        } catch (Exception e) {
            Toast.makeText(MagicBorrowApplication.getInstance(), e.getMessage(), Toast.LENGTH_SHORT).show();
        }
        Log.i("POST", js.toString());
        return js;
    }

    public static JSONObject mapToJson1(HashMap<String, String> map) {
        if (map == null || map.isEmpty()) {
            return null;
        }

        Gson gson = new Gson();
        String s = gson.toJson(map);
        JSONObject js = null;
        try {
            js = new JSONObject(s);

        } catch (Exception e) {
            Toast.makeText(MagicBorrowApplication.getInstance(), e.getMessage(), Toast.LENGTH_SHORT).show();
        }
        Log.i("POST", js.toString());
        return js;
    }


    public static String FormartString(String org, int length) {
        if (org.length() <= length) {
            return org;
        }
        org = org.substring(0, length - 1);
        return org + "...";
    }


}
